package com.big.security.service.impl;

import com.big.common.result.Result;
import com.big.model.model.patient.PatientDsn;
import com.big.patient.PatientFeignClient;
import com.big.security.domain.auth.Role;
import com.big.security.mapper.AuthMapper;
import com.big.security.domain.auth.UserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * 登陆身份认证
 */
@Component(value="CustomUserDetailsService")
public class CustomUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    PatientFeignClient patientFeignClient;

    private final AuthMapper authMapper;

    public CustomUserDetailsServiceImpl(AuthMapper authMapper) {
        this.authMapper = authMapper;
    }

    @Override
    public UserDetail loadUserByUsername(String name) throws UsernameNotFoundException {
        UserDetail userDetail=new UserDetail();
        PatientDsn patientDsn = patientFeignClient.findInfoDns(name);
        System.out.println("loadUserByUsername()方法:"+patientDsn.getUId());
        if ( patientDsn.getPhone() == null) {
            throw new UsernameNotFoundException(String.format("No userDetail found with username '%s'.", name));
        }
        Role role = authMapper.findRoleByUserId(patientDsn.getUId());
        System.out.println(role);
        userDetail.setId(patientDsn.getUId());
        userDetail.setUsername(patientDsn.getPhone());
        userDetail.setPassword(patientDsn.getPassword());
        userDetail.setRole(role);
        System.out.println("loadUserByUsername()方法:"+userDetail);
        return userDetail;
    }
}
